/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.manager;

import entity.Construct;
import entity.Ownership;
import entity.OwnershipPK;
import entity.Player;
import java.util.Collection;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author antonio
 */
@TransactionManagement(TransactionManagementType.CONTAINER)
@Stateless
public class ConstructManagerBean implements ConstructManagerBeanLocal {

    @PersistenceContext(unitName = "BrowserGame-ejbPU")
    private EntityManager entityManager;

    //provvisorio
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @Override
    public void initConstructs(String username) {
        Player player = entityManager.find(Player.class, username);
        Collection<Construct> constructs = findAll();
        System.out.print(constructs);
        for (Construct c : constructs) {
            OwnershipPK pk = new OwnershipPK(player.getUsername(), c.getName());
            Ownership ownership = new Ownership(pk);
            ownership.setPlayer1(player);
            ownership.setConstruct1(c);
            ownership.setAmount(c.getInitAmount());
            entityManager.persist(ownership);
        }
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @Override
    public Construct findConstruct(String name) {
        return entityManager.find(Construct.class, name);
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @Override
    public void createConstructs(String username, String construct, int amount) {
        OwnershipPK pk = new OwnershipPK(username, construct);
        Ownership ownership = entityManager.find(Ownership.class, pk);
        if (amount > ownership.getUncomplited()) {
            throw new IllegalArgumentException("La quantità di costrutti incompleti è minore della quantità da aggiungere");
        }
        ownership.setUncomplited(ownership.getUncomplited() - amount);
        ownership.setAmount(ownership.getAmount() + amount);
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @Override
    public void destroyConstructs(String username, String construct, int amount) {
        OwnershipPK pk = new OwnershipPK(username, construct);
        Ownership ownership = entityManager.find(Ownership.class, pk);
        if (amount > ownership.getAmount() - ownership.getOccupied()) {
            throw new IllegalArgumentException("La quantità di costrutti è minore della quantità da distruggere");
        }
        ownership.setAmount(ownership.getAmount() - amount);
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @Override
    public void beginConstructs(String username, String construct, int amount) {
        OwnershipPK pk = new OwnershipPK(username, construct);
        Ownership ownership = entityManager.find(Ownership.class, pk);
        if (ownership == null) {
            createOwnership(username, construct, 0, 0, amount);
        } else {
            ownership.setUncomplited(ownership.getUncomplited() + amount);
        }
    }

    private void createOwnership(String username, String construct, int amount, int occupied, int uncomplited) {
        OwnershipPK pk = new OwnershipPK(username, construct);
        Ownership ownership = new Ownership(pk);
        ownership.setPlayer1(entityManager.find(Player.class, username));
        ownership.setConstruct1(entityManager.find(Construct.class, construct));
        ownership.setAmount(amount);
        ownership.setOccupied(occupied);
        ownership.setUncomplited(uncomplited);
        entityManager.persist(ownership);
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @Override
    public void occupiesConstructs(String username, String construct, int amount) {
        OwnershipPK pk = new OwnershipPK(username, construct);
        Ownership ownership = entityManager.find(Ownership.class, pk);
        ownership.setOccupied(ownership.getOccupied() + amount);
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    @Override
    public void releaseConstructs(String username, String construct, int amount) {
        OwnershipPK pk = new OwnershipPK(username, construct);
        Ownership ownership = entityManager.find(Ownership.class, pk);
        if (ownership.getOccupied() - amount < 0) {
            ownership.setOccupied(0);
        } else {
            ownership.setOccupied(ownership.getOccupied() - amount);
        }
        System.out.println(pk + " " + ownership + " " + ownership.getOccupied());
    }

    @Override
    public Collection<Construct> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = entityManager.getCriteriaBuilder().createQuery();
        cq.select(cq.from(Construct.class));
        return entityManager.createQuery(cq).getResultList();
    }
}
